import Vue from 'vue'
import VueRouter from 'vue-router'
import Main from '../views/Main.vue'

import store from '../store'


Vue.use(VueRouter)

const routes = [
    {
        path: '/login',
        name: 'login',
        component: () => import('@/views/login.vue')
    },
    {
        path: '/iotManage',
        name: 'iotManage',
        redirect: '/iotManage/doorManage',
        component: Main,
        children: [
            {
                path: 'doorManage',
                name: 'iotManage_doorManage',
                component: () => import('@/views/iotManage/doorManage.vue')
            },
            {
                path: 'doorDetail',
                name: 'iotManage_doorDetail',
                component: () => import('@/views/iotManage/doorDetail.vue')
            },
            {
                path: 'iot',
                name: 'iotManage_iot',
                component: () => import('@/views/iotManage/iot.vue')
            },
            {
                path: 'iotDetail',
                name: 'iotManage_iotDetail',
                component: () => import('@/views/iotManage/iotDetail.vue')
            },
        ]
    },
    {
        path: '/system',
        name: 'system',
        redirect: '/system/account',
        component: Main,
        children: [
            {
                path: 'account',
                name: 'system_account',
                component: () => import('@/views/system/account.vue')
            },
            {
                path: 'company',
                name: 'system_company',
                component: () => import('@/views/system/company.vue')
            },
        ]
    },
    {
        path: '**',
        redirect: '/iotManage'
    },
]

const router = new VueRouter({
  routes
})


// 获取用户权限菜单
router.beforeEach((to, from, next) => {

    if (!/login|home/.test(to.name)) {
        if (!store.state.user.UserInfo || !store.state.user.UserInfo.id) {
            next({
                replace: true,
                name: "login"
            });
            return;
        }
    }

    next();
});

export default router
